#pragma once
#include "ThreadCache.h"
#include "Common.h"
#include"PageCache.h"
#include"ObjectPool.h"
static ObjectPool<ThreadCache> threadCachePool;
static void *ConcurrentAlloc(size_t size)
{
    if (size > MAX_BYTEs)
    {
        size_t alignSize=Sizeclass::reoundUp(size);
        size_t kpage=alignSize>>PAGE_SHIFT;
        PageCache::Instance()->_pagemtx.lock();
        Span*span=PageCache::Instance()->newpage(kpage);
        span->_objsize=size;
        PageCache::Instance()->_pagemtx.unlock();
        void *ptr=(char*)(span->_pageID<<PAGE_SHIFT);
        return ptr;
    }
    else
    {
        if (pTLSThreadCache == nullptr)
        {
            pTLSThreadCache = threadCachePool.New();
        }
        return pTLSThreadCache->Allocate(size);
    }
}
static void ConcurrentFree(void *ptr)
{ 
    Span*span=PageCache::Instance()->MapObjectTospan(ptr);
    int size=span->_objsize;
    if(size > MAX_BYTEs){
        //可能是找PageCache拿的大于128页
        Span*span=PageCache::Instance()->MapObjectTospan(ptr);
        PageCache::Instance()->_pagemtx.lock();
        PageCache::Instance()->ReleaseToPageCache(span);
        PageCache::Instance()->_pagemtx.unlock();

    }
    else{
    assert(pTLSThreadCache != nullptr);
    pTLSThreadCache->Deallocate(ptr, size);
    }
}